package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class StageItemDecoration extends LinearItemDecoration {

    private int verticalDivider;

    public StageItemDecoration(int divider) {
        super(divider);
    }

    public StageItemDecoration(int divider, int color) {
        super(divider, color);
    }

    public StageItemDecoration(int divider, @Nullable Drawable drawable) {
        super(divider, drawable);
    }
//    public GridItemDecoration(RecyclerView rcyList, int divider) {
//        this(rcyList, divider, 0);
//    }

//    public GridItemDecoration(RecyclerView rcyList, int divider, int color) {
//        super(rcyList, divider, color);
//        init();
//    }
//
//    public GridItemDecoration(RecyclerView rcyList, int divider, Drawable drawable) {
//        super(rcyList, divider, drawable);
//        init();
//    }

    public StageItemDecoration setVerticalDivider(int divider) {
        this.verticalDivider = divider;
        return this;
    }

    public int getVerticalDivider() {
        return verticalDivider;
    }

    //    @Override
//    public GridItemDecoration build() {
//        if (spanCount > 1) {
//            if (orientation == LinearLayoutManager.VERTICAL && paddingLeft > 0 && rcyList.getPaddingLeft() == 0) {
//                rcyList.setPadding(paddingLeft, rcyList.getPaddingTop(), rcyList.getPaddingTop(), rcyList.getPaddingBottom());
//            } else if (orientation == LinearLayoutManager.HORIZONTAL && paddingTop > 0 && rcyList.getPaddingTop() == 0) {
//                rcyList.setPadding(rcyList.getPaddingLeft(), paddingTop, rcyList.getPaddingTop(), rcyList.getPaddingBottom());
//            }
//        }
//        return this;
//    }

    @Override
    protected void drawDivider(@NonNull Canvas canvas, @NonNull RecyclerView parent) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (adapter == null || !(manager instanceof StaggeredGridLayoutManager)) return;
        int spanCount = ((StaggeredGridLayoutManager) manager).getSpanCount();
        int orientation = ((StaggeredGridLayoutManager) manager).getOrientation();
        if (spanCount <= 1) {
            super.drawDivider(canvas, parent);
            return;
        }
        int left;
        int top;
        int right;
        int bottom;
        int vLeft;
        int vTop;
        int vRight;
        int vBottom;
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) child.getLayoutParams();
            int viewAdapterPosition = params.getViewAdapterPosition();
            int viewType = adapter.getItemViewType(viewAdapterPosition);
            if (viewType != 0) continue; //为0时才绘制
            if (orientation == RecyclerView.VERTICAL) {
                left = child.getLeft();
                top = child.getBottom();
                right = child.getRight();
                bottom = top + getDivider();
                vLeft = child.getRight();
                vTop = child.getTop();
                vRight = vLeft + verticalDivider;
                vBottom = child.getBottom() + getDivider();
            } else {
                left = child.getLeft();
                top = child.getBottom();
                right = child.getRight();
                bottom = top + getDivider();
                vLeft = child.getRight();
                vTop = child.getTop();
                vRight = vLeft + verticalDivider;
                vBottom = child.getBottom() + getDivider();
            }
            if (getDrawable() != null) {
                getDrawable().setBounds(left, top, right, bottom);
                getDrawable().draw(canvas);
                getDrawable().setBounds(vLeft, vTop, vRight, vBottom);
                getDrawable().draw(canvas);
            } else if (getPaint() != null) {
                canvas.drawRect(left, top, right, bottom, getPaint());
                canvas.drawRect(vLeft, vTop, vRight, vBottom, getPaint());
            }
        }
    }

    @Override
    protected void configureItemOutRect(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (adapter == null || manager == null) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        if (!(manager instanceof StaggeredGridLayoutManager)) {
            super.configureItemOutRect(outRect, view, parent);
            return;
        }
        int spanCount = ((StaggeredGridLayoutManager) manager).getSpanCount();
        if (spanCount <= 1) {
            super.configureItemOutRect(outRect, view, parent);
            return;
        }
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        int viewAdapterPosition = params.getViewAdapterPosition();
        int viewType = adapter.getItemViewType(viewAdapterPosition);
        if (!containsType(viewType)) { //包含的Item类型时才绘制
            outRect.set(0, 0, 0, 0);
            return;
        }
        int spanIndex = params.getSpanIndex();
        int count = adapter.getItemCount();
        int left = 0;
        int top = 0;
        int right;
        int bottom = 0;
        if ((spanIndex + 1) % spanCount == 1) {//当前行第一条
            left = getPadding().left;
        }
        if (spanIndex < spanCount) {//第一排
            top = getPadding().top;
            if (isStartDivider()) {//第一条顶部是否需要添加分割线
                top += getDivider();
            }
        }
        if ((spanIndex + 1) % spanCount == 0) {//当前行第spanCount条
            right = getPadding().right;
        } else {
            right = getVerticalDivider();
        }
        if (isEndLine(viewAdapterPosition, spanCount, count)) {//最后一排
            bottom = getPadding().bottom;
            if (isEndDivider()) {//最后一条底部是否需要添加分割线
                bottom += getDivider();
            }
        } else {
            bottom = getDivider();
        }
        outRect.set(left, top, right, bottom);
    }

    /**
     * 是否是最后一排
     *
     * @param index 下标
     * @param count 总数
     */
    private boolean isEndLine(int index, int spanCount, int count) {
        if (count <= spanCount) return true; //不足一排
        int lines = count / spanCount;
        if (lines % spanCount > 0)
            lines++;
        return (index + 1) > ((lines - 1) * spanCount);
    }
}
